<template>
  <div>
    单品管理
    <div class="data">
      <div class="searchForm">
        <el-form :inline="true" :model="params" class="demo-form-inline">
          <el-form-item label="单品">
            <el-input
               v-model="params.name"
               placeholder="按单品名称查找"
               class="block">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
               icon="el-icon-search"
               @click="getList(1)"
               class="block">
               查询</el-button>

               <el-button
               type="primary"
               icon="el-icon-plus"
               @click="add"
               >增加</el-button>

              <el-button
               type="danger"
               icon="el-icon-delete"
               @click="removeMore"
               >批量删除</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="message">
      <div class="message1">
         <el-table :data="rows" stripe style="width: 100%" @selection-change="handleSelectionChange">
           <el-table-column type="selection" width="55"></el-table-column>
           <el-table-column prop="skuId" label="单品id"> </el-table-column>
           <el-table-column prop="salePrice" label="售价">
           </el-table-column>
           <el-table-column prop="originalPrice" label="原价"> </el-table-column>
           <el-table-column prop="productId" label="商品管理id" min-width="100px"> </el-table-column>
            <el-table-column prop="productName" label="单品名称"> </el-table-column>
            <el-table-column prop="img" label="单品图片">
            <template slot-scope="scope">
              <img style="width: 50px; height: 50px" v-if="scope.row.img" :src="scope.row.img"/>
            </template>
            </el-table-column>
           <el-table-column prop="props" label="材料"> </el-table-column>
          <el-table-column prop="skuSpecification" label="单品描述"  min-width="200px"> </el-table-column>
          <el-table-column label="操作" width="320">
             <template slot-scope="scope">
               <el-button
               type="primary"
               size="mini"
               icon="el-icon-edit"
               style="margin-right:16px;"
               @click="edit(scope.row)"
               >编辑</el-button>
               <el-popconfirm
                 title="确定删除此数据吗？"
                 @confirm="remove(scope.row)"
               >
                 <el-button 
                 type="danger"
                 size="mini"
                 icon="el-icon-delete"
                 slot="reference">删除</el-button>
               </el-popconfirm>
             </template>
           <template>
             <el-button
               type="primary"
               size="mini"
               icon="el-icon-edit"
               >编辑</el-button>
               <el-button
               type="danger"
               size="mini"
               icon="el-icon-refresh-right"
               >删除</el-button>
           </template>
           </el-table-column>
         </el-table>
      </div>
    </div>
    <div class="pagination">
        <el-pagination
           @size-change="sizeChange"
           @current-change="getList"
           :current-page="params.pageNum"
           :page-sizes="[5,10, 100, 200, 300, 400]"
           :page-size="params.pageSize"
           layout="total, sizes, prev, pager, next, jumper"
           :total="total">
        </el-pagination>
    </div>
    <el-dialog :title="form._id ? '修改' : '新增'" :visible.sync="dialogFormVisible" width="400px">
        <el-form :model="form" ref="form" :rules="rules" label-width="100px">
             <el-form-item label="单品id" prop="skuId">
          <el-input type="text" v-model="form.skuId"></el-input>
        </el-form-item>
         <el-form-item label="售价" prop="salePrice">
          <el-input type="text" v-model="form.salePrice"></el-input>
        </el-form-item>
         <el-form-item label="原价" prop="originalPrice">
          <el-input type="text" v-model="form.originalPrice"></el-input>
        </el-form-item>
         <el-form-item label="商品管理中id" prop="productId">
          <el-input type="text" v-model="form.productId"></el-input>
        </el-form-item>
         <el-form-item label="单品名称" prop="productName">
          <el-input type="text" v-model="form.productName"></el-input>
        </el-form-item>
            <el-form-item label="轮播图片" prop="img">
              <div @click="openFile" class="imgStyle">
                <img style="max-width:100%;max-height:100%" v-if="form.img" :src="form.img" />
                <span v-else>+</span>
              </div>
              <input ref="file" style="display:none" multiple type="file" accept="image/*" @change="changeFile" />
            </el-form-item>
            <el-form-item label="材料" prop="props">
          <el-input type="text" v-model="form.props"></el-input>
        </el-form-item>
        <el-form-item label="单品名称" prop="skuSpecification">
          <el-input type="text" v-model="form.skuSpecification"></el-input>
        </el-form-item>
            <el-form-item label="类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择">
                 <el-option
                 v-for="type in types"
                 :key="type.value"
                 :label="type.label"
                 :value="type.value">
                 </el-option>
              </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button @click="submit" type="primary">确定</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import api from "@/api";
export default {
  name: "shouyepersonproduct",
  components: {},
  data() {
    return {
      //查询参数
      params: {
        productName:"",
        title: "",
        type: "personproduct",
        pageNum: 1,
        pageSize: 5,
      },
      rows: [],
      total: 0,
      dialogFormVisible: false,
      form: {
        img: "",
        type: "personproduct",
        url: "",
        title: "",
        _id: 0,
      },
      rules: {
        productName: [{ required: true, message: "单品名不能为空", trigger: "blur" }],
        skuId: [
          { required: true, message: "单品id不能为空", trigger: "blur" },
        ],
      },

      types: [
        { label: "品牌", value: "brnad" },
        { label: "分类", value: "classify" },
        { label: "商品", value: "peoducts" },
        { label: "单品", value: "personproduct" },
      ],
      selecteds: [],
    };
  },
  methods: {
    //获取列表
    getList(pageNum) {
      if (pageNum) this.params.pageNum = pageNum;
      this.$http({
        url: "/admin/index/personproduct",
        params: this.params,
      }).then((res) => {
        this.total = res.total;
        this.rows = res.rows;
      });
    },
    //修改pageSize
    sizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.pageNum = 1;
      this.getList(1);
    },
    //新增
    add() {
      //初始化
      this.form = {
        img: "",
        type: "personproduct",
        url: "",
        title: "",
        _id: 0,
      };
      this.dialogFormVisible = true;
    },
    //修改数据
    edit(row) {
      this.form = row;
      this.dialogFormVisible = true;
    },
    //删除数据
    remove(row) {
      this.$http({
        url: "/admin/index/personproduct",
        method: "DELETE",
        params: { indexId: row._id },
      }).then((res) => {
        if (!res.errorMsg) {
          this.getList(1);
        }
      });
    },
    handleSelectionChange(rows) {
      //存储选中数据
      this.selecteds = rows;
    },

    //批量删除
    removeMore() {
      //获取备选中数据
      let checkedRowsPromise = [];
      let success = [],
        fail = [];
      for (let i = 0; i < this.selecteds.length; i++) {
        checkedRowsPromise.push(
          new Promise((resovle) => {
            let row = this.selecteds[i];
            this.$http({
              url: "/admin/index/personproduct",
              method: "DELETE",
              params: { indexId: row._id },
            }).then((res) => {
              if (!res.errorMsg) {
                success.push(row._id);
              } else {
                fail.push(row._id);
              }
              resovle();
            });
          })
        );
      }
      this.$confirm("此操作将删除选中的数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //删除
        Promise.all(checkedRowsPromise).then(() => {
          this.$message({
            message: `批量删除完成，成功删除${success.length}条数据,删除失败${fail.length}条数据`,
            duration: 3000,
          });
          this.getList(1);
        });
      });
    },

    changeFile(e) {
      let files = e.target.files;
      let formDate = new FormData();
      formDate.append("file", files[0]);

      this.$http({
        url: "/upload",
        method: "POST",
        data: formDate,
      }).then((res) => {
        var path = res.path.split("\\");
        path = path[0] + "/" + path[1];
        console.log(path);
        if (res.path) this.form.img = api.baseURL + "/" + path;
        console.log(res);
      });
    },
    openFile() {
      let fileElement = this.$refs.file;
      fileElement.click();
    },

    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.indexId = this.form._id;
          this.$http({
            url: "/admin/index/personproduct",
            method: this.form._id ? "PUT" : "POST", //通过indexId判断是新增还是修改
            data: this.form,
          }).then((res) => {
            if (!res.errorMsg) {
              this.dialogFormVisible = false;
              this.getList(this.form.indexId ? undefined : 1);
            }
          });
        }
      });
    },
  },

  created() {
    this.getList();
  },
  mounted() {},
};
</script>

<style>
.data{
  margin-top: 8%;
  width: 90%;
  margin-left: 3%;
  background-color: white;
}
.message{
  margin-top: 2%;
  margin-left: 3%;
  width: 90%;
}
.pagination{
  margin-top: 2%;
  margin-left: 3%;
}
.imgStyle {
  width: 100px;
  height: 100px;
  font-size: 32px;
  border: 1px solid #ccc;
  text-align: center;
  line-height: 100px;
  overflow: hidden;
}
</style>
